package com.ontheroad.mysql.topic.service.app;

import cn.modoumama.page.PageObject;
import com.ontheroad.api.request.TopicRequest;
import com.ontheroad.api.response.CircleResponse;
import com.ontheroad.api.response.Response;
import com.ontheroad.circle.model.CircleModel;
import com.ontheroad.circle.service.CircleService;
import com.ontheroad.mysql.circle.mapper.CircleMapper;
import com.ontheroad.topic.model.TopicModel;
import com.ontheroad.topic.service.TopicService;
import com.ontheroad.topic.service.TopicUserService;
import com.ontheroad.topic.service.app.AppTopicService;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 话题相关接口实现类
 * <p>
 * Created by kedong on 2017/10/12 0012.
 */
@Service
@Transactional
public class AppTopicServiceImpl implements AppTopicService {

    private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private TopicService topicService;

    @Autowired
    private TopicUserService topicUserService;

    @Autowired
    private CircleService circleService;

    @Autowired
    private CircleMapper circleMapper;

    @Override
    public Response list(TopicRequest request) {
        Response response = new Response();
        PageObject po = new PageObject();
        po.setCurrPage(request.getPageNum());
        po.setPageSize(request.getPageSize());
        po.addCondition("key", request.getKey());
        List<TopicModel> list = topicService.findModelsByCondition(po);
        response.addResultData("list", list);
        return response;
    }

    /**
     * 话题详情，根据话题标签查询动态列表
     *
     * @param request
     * @return
     */
    @Override
    public Response detail(TopicRequest request) {
        Response response = new Response();
        Integer topicId = request.getTopicId();
        PageObject po = new PageObject();
        po.setCurrPage(request.getPageNum());
        po.setPageSize(request.getPageSize());
        List<CircleModel> list = circleMapper.findModelsByTopicId(topicId, new RowBounds(po.getOffset(), po.getPageSize()));
        List<CircleResponse> circleResponses = circleService.getCircleList(list, null, false, request.getUserId().intValue());
        response.addResultData("list", circleResponses);
        return response;
    }

    @Override
    public Response createTopic(TopicRequest request) {
        Response response = new Response();
        TopicModel topicModel = new TopicModel();
        topicModel.setName(request.getName());
        topicModel.setDescription(request.getDescription());
        topicModel.setCreateTime(new Date());

        topicModel = topicService.save(topicModel);
        response.addResultData("model", topicModel);
        return response;
    }
}
